package com.eking.services;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.alibaba.fastjson2.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiAttendanceGetupdatedataRequest;
import com.dingtalk.api.request.OapiAttendanceListRequest;
import com.dingtalk.api.response.OapiAttendanceGetupdatedataResponse;
import com.dingtalk.api.response.OapiAttendanceListResponse;
import com.eking.domain.AtCheckInfoForOpenPo;
import com.eking.mapper.AtCheckInfoForOpenMapper;
import com.eking.tasks.ScheduleTask;
import com.taobao.api.ApiException;
import com.taobao.api.internal.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * 考勤信息
 * Created by hexp on 2023/9/6 20:40
 */
@Component
@Slf4j
public class AttendanceApi {

    @Autowired
    private ScheduleTask  scheduleTask;

    @Autowired
    private AtCheckInfoForOpenMapper atCheckInfoForOpenMapper;


    /**
     * 获取考勤数据集合
     * @param startDate 开始时间
     * @param endDate 结束时间
     * startDate与endDate时间相差不能超过7天
     */
    public void attendanceList(String startDate, String endDate, List<String> userIds){
        //usersApi.getMemberusersList().get()
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/list");
        OapiAttendanceListRequest req = new OapiAttendanceListRequest();
        req.setWorkDateFrom(startDate);
        req.setWorkDateTo(endDate);
        req.setUserIdList(userIds);
        req.setOffset(0L);
        req.setLimit(Long.MAX_VALUE);
        req.setIsI18n(true);
        OapiAttendanceListResponse rsp = null;
        try {
            rsp = client.execute(req, scheduleTask.getToken());
        } catch (ApiException e) {
            log.error("获取考勤时间集合失败",e);
            throw new RuntimeException(e);
        }
        List<OapiAttendanceListResponse.Recordresult> recordresults = Optional.ofNullable(rsp.getRecordresult()).orElse(Collections.emptyList());
    }


    /**
     * 获取用户考勤数据
     *
     * @param workDate 查询日期
     * @param userId   用户id
     */
    public void getAttendanceDetail(Date workDate, String userId) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getupdatedata");
        OapiAttendanceGetupdatedataRequest req = new OapiAttendanceGetupdatedataRequest();
        req.setUserid(userId);
        req.setWorkDate(workDate);
        OapiAttendanceGetupdatedataResponse rsp = null;
        try {
            rsp = client.execute(req, scheduleTask.getToken());
        } catch (ApiException e) {
            log.error("获取【{}】用户【{}】日期的考勤数据异常", userId, StringUtils.formatDateTime(workDate));
            throw new RuntimeException(e);
        }
        Assert.isTrue(rsp.isSuccess(), "查询用户考勤数据失败" + rsp.getErrmsg());
        AtCheckInfoForOpenPo atCheckInfoForOpenPo = JSONObject.parseObject(JSONObject.toJSONString(rsp.getResult()),
                AtCheckInfoForOpenPo.class);
        atCheckInfoForOpenMapper.insert(atCheckInfoForOpenPo);
    }

}
